<?php
@session_start();
class Thankyou extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index(){
       // echo 'in';exit;
         $sql = new sql();
         $sql->connect();
         $GETID = getId();
        
		 $paymentArray = $_SESSION['paymentArray'];
         if (array_key_exists('1', getId())) {
            $GETID = getId();
            $user_id = $GETID[1];
			$payment_optn = $GETID[2];
            $UserArray = $sql->fetch("tbl_user", array("user_id" => $user_id));
            $last_quickbook_id = $UserArray['quick_book_id'];
            $user_firstname = $UserArray['user_firstname'];
			$user_email = $UserArray['user_email'];
			$total_Amount = 0;
            foreach($_SESSION['shoping_cart'] as $key => $value){
                $total_Amount = $total_Amount + 
				($value['prd_qty'] * $value['prd_price']);
            }
			
			
		   $pt_type = "";
           $quickbook_ref_id = 0;
            
			foreach ($paymentArray as $p_key => $p_value) {
                if ($payment_optn == $p_key) {
                    //$mij_access_key = $p_value['mij_access_key'];
                    $pt_type = $p_value['Name'];
                }
            }
			
			
			
			/* if($payment_optn == '1'){
               $pt_type = 'CHARGE';
               $quickbook_ref_id = 4;
            }else if($payment_optn == '2'){
                $pt_type = 'GPRO';
                $quickbook_ref_id = 5;
            }*/
			
			
			
			/*else if($payment_optn == '4'){
                $pt_type = 'Mijireh Store 4';
                 $quickbook_ref_id = 7;
            }*/
			//$_GET['order_number'] =
			$orderNum = $_GET['order_number'];
			//$orderNum =  strtotime("now");
			$transaction_id = $pt_type." - ".$orderNum;
			
			
			 $paymentdata = array("pt_type" => $pt_type,
                   // "pt_via" => 'Mijireh',
                     // "pt_cardno" => $txtCreditcardno,
                    //"pt_expmonth" => $drpExpMonth,
                    //"pt_expyear" => $expyear,
                    "pt_user_id" => $user_id,
                    "pt_transaction_id" => $orderNum,
                    "pt_total_amount" => $total_Amount,
                    "pt_createDate" => date('Y-m-d h:i:s'),
                    "pt_status" => 'active',
                );
            $sql->insert('payment_order', $paymentdata);
            $lastpaymentid = $sql->last_id();
			
			
            foreach($_SESSION['shoping_cart'] as $key => $value){
                $orderdata = array("or_user_id" => $user_id,
                    "or_product_id" => $value['prd_id'],
                    "or_price" => $value['prd_qty'] * $value['prd_price'],
                    "or_createdate" => date('Y-m-d h:i:s'),
                    "or_status" => 1,
                );
                $insert_order = $sql->insert('tbl_order', $orderdata);
				
			$keyArray = $sql->fetch("tbl_metadata", array("mtd_key" => 'payment_key'));
			$key = $keyArray['mtd_value'];	
				
			if ($key == count($paymentArray)) {
				$updatedKey = 1;
			} else {
				$updatedKey = $key + 1;
			}
				
				$update = $sql->update('tbl_metadata', array("mtd_value" => $updatedKey), array("mtd_key" => 'payment_key'));
				
			
            }
            
            include('quickbooks/salerecepit.php');
            
         }
        $this->view->render('thankyou');
    }
}